/*
 * CSplashScence.cpp
 *
 *  Created on: Apr 4, 2014
 *      Author: 01nvchuong
 */

#include "AppDelegate.h"
#include "CSplashScene.h"
#include "Constants.h"
#include "CGameManager.h"

using namespace cocos2d;

CCScene* CSplashLayer::scene()
{
	CCScene *scene = CCScene::create();

	CSplashLayer *layer = CSplashLayer::create();

	scene->addChild(layer);

	return scene;
}


bool CSplashLayer::init()
{

	if (!CCLayer::init()) {
		return false;
	}

	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
	
	CCSprite* bgSprite = CCSprite::create(BACKGROUND_IMAGE);
	bgSprite->setPosition(ccp(visibleSize.width / 2, visibleSize.height / 2));
	this->addChild(bgSprite, indexBackground);

	//CCLabelTTF* title = CCLabelTTF::create(TOUCH_TO_PLAY, gameOverFont, FONTSIZE_GAMEOVER);
	//title->setColor(ccc3(255,255,255));
	//title->setPosition(ccp(visibleSize.width / 2, visibleSize.height * 2 / 3));
	//this->addChild(title, indexText);
	
	CCSprite* titleSprite = CCSprite::create(GAME_TITLE);
	titleSprite->setPosition(ccp(visibleSize.width / 2, visibleSize.height * 8 / 10));
	this->addChild(titleSprite, indexText);

	CCSprite* tutorialSprite = CCSprite::create(GAME_TUTORIAL);
	tutorialSprite->setPosition(ccp(visibleSize.width / 2, visibleSize.height / 2));
	this->addChild(tutorialSprite, indexText);


	CCMenuItemImage* btnPlay = CCMenuItemImage::create(GAME_PLAY_BUTTON, GAME_PLAY_BUTTON, this, menu_selector(CSplashLayer::onPlayClick));
	int playWidth = btnPlay->getContentSize().width;
	int playHeight = btnPlay->getContentSize().height;

	int delta = (visibleSize.width - 2 * playWidth) / 3;

	int playX = delta + playWidth / 2;
	int playY = visibleSize.height / 4;

	btnPlay->setPosition(ccp(playX, playY));

	// TODO add rate function 
	CCMenuItemImage* btnRate = CCMenuItemImage::create(GAME_RATE_BUTTON, GAME_RATE_BUTTON, this, menu_selector(CSplashLayer::onRateClick));
	int shareX = delta * 2 + 3 * playWidth / 2;
	int shareY = visibleSize.height / 4;
	btnRate->setPosition(ccp(shareX, shareY));

	CCMenu* pMenu = CCMenu::create(btnPlay, btnRate, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, indexMenu);

	//play background music
	CGameManager::Instance()->playSound();

    //this->scheduleOnce(schedule_selector(CSplashLayer::RunGameScreen), 3.0f);
	this->setTouchEnabled(true);
	//this->setKeypadEnabled(true);
	
	return true;
}

void CSplashLayer::onPlayClick()
{
	CCLog("onPlayClick");
	CGameManager::Instance()->RunScene(SCENE_GAME);
}

void CSplashLayer::onRateClick()
{
	CCLog("onRateClick");
	((AppDelegate*) CCApplication::sharedApplication())->rateThisApp();
}

// void CSplashLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
// {
// CCLog("ccTouchesEnded");
// this->RunGameScreen();
// }


//void CSplashLayer::keyBackClicked(void) {
//	CCLog("keyBackClicked");
//	programExit(NULL);
//}
//
//void CSplashLayer::programExit(CCObject* pSender) {
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
//	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
//#else
//	CCDirector::sharedDirector()->end();
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
//	exit(0);
//#endif
//#endif
//}
